#include "Console.h"

#include "SGD Wrappers\CSGD_TextureManager.h"
#include "SGD Wrappers\CSGD_DirectInput.h"

#include "Game.h"
#include "GamePlayState.h"
#include "MinigameState.h"
#include "CreatePickUpMessage.h"
#include "PickUp.h"
#include "MessageSystem.h"
#include "Player.h"
#include "UpgradeState.h"

int Console::ImageID = -1;
int Console::Count = 0;

int Console::buttonA = -1;

// Constructor
Console::Console(void) 
{
	// If this is the first chest, load the image
	if( ImageID == -1 )
		ImageID = CSGD_TextureManager::GetInstance()->LoadTexture(_T("graphics/chest.png"),D3DCOLOR_XRGB(0,255,0));

	// Set the image id
	SetTextureID(ImageID);

	// Increase the count
	++Count;

	// Set the chest to be closed
	CurrentState = CLOSED;

	// Set it to be a chest
	type = OBJ_CHEST;

	// There's no player nearby when it spawns
	PlayerNear = false;

	if( buttonA == -1 )
		buttonA = CSGD_TextureManager::GetInstance()->LoadTexture(_T("Graphics/Menu Images/ButtonA.png"));
}

// Destructor
Console::~Console(void) 
{
	// Decrease the cout
	--Count;

	ObjectManager::GetInstance()->RemoveObject(this);

	// If there's no more chests, unload the image
	if( Count == 0 && ImageID != -1 )
	{
		CSGD_TextureManager::GetInstance()->UnloadTexture(ImageID);
		ImageID = -1;
	}
	if( Count == 0 && buttonA != -1 )
	{
		CSGD_TextureManager::GetInstance()->UnloadTexture(buttonA);
		buttonA = -1;
	}
}

// Update
void Console::Update(float elapsed)
{
	GamePlayState* game = GamePlayState::GetInstance();
	if( PlayerNear && CSGD_DirectInput::GetInstance()->JoystickButtonPressed(0, playerNum) && game->inUpgradeState == false)
	{
		game->inUpgradeState = true;
		if(playerNum == 0)
			UpgradeState::GetInstance()->SetPlayer(playerNum, game->GetPlayer());
		else
			UpgradeState::GetInstance()->SetPlayer(playerNum, game->GetPlayer2());

		//if(UpgradeState::GetInstance()->IsFirstTime())
		UpgradeState::GetInstance()->InitItems();

		UpgradeState::GetInstance()->Enter();
		//MinigameState::GetInstance()->StartGame(playerNum);
	}

	// Reset whether a player is near or not
	PlayerNear = false;

	//if( game->inUpgradeState )
	//{
	//	if( MinigameState::GetInstance()->Won )
	//	{
	//		CurrentState = OPEN;
	//		// Create some pickups
	//		CreatePickUpMessage* loot = new CreatePickUpMessage(METAL,rand()%10 + 10,this);
	//		MessageSystem::GetInstance()->SendMsg(loot);
	//		loot = new CreatePickUpMessage(CIRCUITRY,rand()%10 + 10,this);
	//		MessageSystem::GetInstance()->SendMsg(loot);
	//		loot = NULL;
	//		InGame = false;
	//	}
	//}
}

// Draw
void Console::Render()
{
	// The area of the picture to render
	RECT render = {32,32,64,64};
	/*switch ( CurrentState )
	{
	case CLOSED:
		break;
	case OPEN:
		render.left = 32;
		render.right = 64;
		break;
	case BROKE:
		render.top = 32;
		render.bottom = 64;
		break;
	}*/

	// Draw the chest
	CSGD_TextureManager::GetInstance()->Draw(GetTextureID(),(int)GetX() - GamePlayState::GetInstance()->GetCamX(),(int)GetY() - GamePlayState::GetInstance()->GetCamY(),1,1,&render);

	// Draw the indicator
	if( PlayerNear )
		CSGD_TextureManager::GetInstance()->Draw(buttonA,(int)GetX() - GamePlayState::GetInstance()->GetCamX(),(int)(GetY()-32) - GamePlayState::GetInstance()->GetCamY(),.5f,.5f);

		//Game::GetInstance()->DrawBitmapFont("A",(int)GetX() + 6 - GamePlayState::GetInstance()->GetCamX(),(int)GetY() - 32 - GamePlayState::GetInstance()->GetCamY(),1,D3DCOLOR_XRGB(0,150,200));
}

// Check collision
bool Console::CheckCollision(IEntity* base)
{
	if( Entity::CheckCollision(base) && base->GetType() == OBJ_PLAYER && CurrentState == CLOSED)
	{
		//Player* p = dynamic_cast<Player*>(base);
		//if(p->GetPlayerNum() == 1)
		//	playerNum = 0;
		//else
		//	playerNum = 1;
		//PlayerNear = true;
		//p->CanActivate(true);
		//return true;
		
		Player* p = dynamic_cast<Player*>(base);
		if(p == GamePlayState::GetInstance()->GetPlayer())
			playerNum = 0;
		else if(p == GamePlayState::GetInstance()->GetPlayer2())
			playerNum = 1;
		PlayerNear = true;
		p->CanActivate(true);
		return true;
	}

	return false;
}

// Get the current state
unsigned char Console::GetState(void)
{
	return CurrentState;
}